﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace ChineseChess.Utils
{
    /// <summary>
    /// 调用引擎
    /// </summary>
    public static class EngineUtil
    {
        /// <summary>
        /// 最佳棋步
        /// fen中包含moves可以避免常將
        /// </summary>
        /// <param name="fen"></param>
        /// <param name="thinkTime"></param>
        /// <returns></returns>
        public static string BestStep(string fen, int thinkTime)
        {
            DateTime start = DateTime.Now;
            Process proEngine = new();

            proEngine.StartInfo.FileName = "eleeye.exe";

            proEngine.StartInfo.CreateNoWindow = true;
            proEngine.StartInfo.RedirectStandardError = true;
            proEngine.StartInfo.RedirectStandardInput = true;
            proEngine.StartInfo.RedirectStandardOutput = true;
            proEngine.StartInfo.UseShellExecute = false;

            proEngine.Start();

            proEngine.StandardInput.WriteLine("ucci");
            proEngine.StandardInput.WriteLine("setoption batch on");
            proEngine.StandardInput.WriteLine("position fen " + fen);
            proEngine.StandardInput.WriteLine("go time " + thinkTime.ToString());
            proEngine.StandardInput.WriteLine("quit");
            string output = proEngine.StandardOutput.ReadToEnd();
            proEngine.Close();

            DateTime end = DateTime.Now;

            Debug.WriteLine("计算耗时:"+(end - start).TotalMilliseconds + "ms");

            return output;
        }

    }
}
